package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesTechnologyFlowType;
import mesweb.mesweb.service.IMesTechnologyFlowTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 工艺流程类型
 * @Author: jeecg-boot
 * @Date:   2021-06-18
 * @Version: V1.0
 */

@Api(tags="工艺流程类型")
@RestController
@RequestMapping("/mes/mesTechnologyFlowType")
public class MesTechnologyFlowTypeController {
	@Autowired
	private IMesTechnologyFlowTypeService mesTechnologyFlowTypeService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<MesTechnologyFlowType> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesTechnologyFlowTypeService.list();
        } else {
            QueryWrapper<MesTechnologyFlowType> queryWrapper = new QueryWrapper<>();
            data = mesTechnologyFlowTypeService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param mesTechnologyFlowType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="工艺流程类型-分页列表查询", notes="工艺流程类型-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesTechnologyFlowType mesTechnologyFlowType,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<MesTechnologyFlowType> queryWrapper = new QueryWrapper<>();
		Page<MesTechnologyFlowType> pageMesTechnologyFlowType = new Page<MesTechnologyFlowType>(page, size);
		IPage<MesTechnologyFlowType> pageList = mesTechnologyFlowTypeService.page(pageMesTechnologyFlowType, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesTechnologyFlowType
	 * @return
	 */
	@ApiOperation(value="工艺流程类型-编辑", notes="工艺流程类型-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody MesTechnologyFlowType mesTechnologyFlowType) {

		boolean result = false;
        if(mesTechnologyFlowType.getId() == 0L) {
			QueryWrapper<MesTechnologyFlowType> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("technology_flow_type_no",mesTechnologyFlowType.getTechnologyFlowTypeNo());
			queryWrapper.or().eq("technology_flow_type_name",mesTechnologyFlowType.getTechnologyFlowTypeName());
			MesTechnologyFlowType mesTechnologyFlow1 = mesTechnologyFlowTypeService.getOne(queryWrapper);
			if(mesTechnologyFlow1 != null){
				String msg = StrUtil.format("编号{}或名称{}已经存在！",mesTechnologyFlow1.getTechnologyFlowTypeNo(),mesTechnologyFlow1.getTechnologyFlowTypeName());
				throw new ApiException(msg);
			}
             result = mesTechnologyFlowTypeService.save(mesTechnologyFlowType);
        }
        else
        {
            result = mesTechnologyFlowTypeService.updateById(mesTechnologyFlowType);
        }
        return CommonResult.success(result);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺流程类型-通过id删除", notes="工艺流程类型-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesTechnologyFlowTypeService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="工艺流程类型-批量删除", notes="工艺流程类型-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesTechnologyFlowTypeIDs") @RequestBody List<Long> ids) {
		this.mesTechnologyFlowTypeService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺流程类型-通过id查询", notes="工艺流程类型-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesTechnologyFlowType mesTechnologyFlowType = mesTechnologyFlowTypeService.getById(id);
		return CommonResult.success(mesTechnologyFlowType);
	}
}
